﻿using System;
using System.Text.Json;
using System.Text.Json.Serialization;

namespace Manon.Core.Commons
{

    public class LongConverter : JsonConverter<long>
    {

        public override long Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
        {
            if (reader.TokenType == JsonTokenType.Number)
            {
                return reader.GetInt64();
            }
            else
            {
                string readerString = reader.GetString();
                if (!string.IsNullOrEmpty(readerString))
                {
                    return long.Parse(readerString);
                }
                return 0;
            }
        }

        public override void Write(Utf8JsonWriter writer, long value, JsonSerializerOptions options)
        {
            string newvalue = value.ToString();
            writer.WriteStringValue(newvalue);
        }
    }
}